<?php 

/**
 * Bootstrap
 */

spl_autoload_register(array('DM_Loader', 'auto_load'));

require_once 'constants.inc';

class Bootstrap implements DM_Application_IBootstrap {
	protected $application;
		
	public $route;
        
        public $cacheManager = null;
	public $db = null;
	
	public function __construct() {
            DM_Debug::get()->enable( true );
            
            DM_log::get()->attach(new DM_Log_File(APPLICATION_PATH.'/logs'));
            
            $this->application = DM_Application::get();

            DM_Registry::write( 'bootstrap', $this );	
	}
	
	public function getApplication() {
            return $this->application;
	}
	
	public function run() {
            $this->loadOptions();

            $this->initCache();

            $this->initDB();

            $this->initTranslate();	

            $this->initPlugins();

            $this->initRouter();
            
            $this->initPreferences();
	}
        
        public function initPreferences() {
            
        }
        
        public function initPlugins() {
            DM_Plugins::addDirectory( APPLICATION_PATH.'/plugins' );
        }
	
	public function loadOptions() {
            $salt = '123asd...!!!eee';
            DM_Registry::write('Configure.salt', $salt );
	}
	
	public function initCache() {
            $cacheTableStructure = $this->cacheManager = new DM_Cache_Manager( '5min', array(
                    'cache_dir' => ROOT_PATH.'/cache/5min',
                    'lifetime' => 300
            ) );
            
            $cacheTableStructure = $this->cacheManager = new DM_Cache_Manager( 'table_structure', array(
                    'cache_dir' => ROOT_PATH.'/cache/table_structure',
                    'lifetime' => 60 // @TODO Кэш структур таблиц (DESCRIBE) после финала увеличить цифру, минимум до 1 часа
            ) );

            $cacheSql = $this->cacheManager = new DM_Cache_Manager( 'sql', array(
                    'cache_dir' => ROOT_PATH.'/cache/sql',
                    'lifetime' => 86400
            ) );

            $cacheUnlim = $this->cacheManager = new DM_Cache_Manager( 'unlim', array(
                    'cache_dir' => ROOT_PATH.'/cache/unlimited',
                    'lifetime' => 100 // @TODO change to 99999999999999
            ) );

            /*$cacheMemcached = $this->cacheManager = new DM_Cache_Manager( 'memcached', array(
                    'class' => DM_CACHE_MEMCACHED,
                    'lifetime' => 24*60*60
            ) );*/

            DM_Registry::set( 'cacheManager', $this->cacheManager );
	}
	
	public function initDB() {
            $this->db = new DM_Db_Adapter( array(
                    'user' => 'root',
                    'password' => '',
                    'database' => 'test',
                    'structureCache' => $this->cacheManager->get('table_structure')
            ) );

            DM_Registry::set( 'db', $this->db );
	}
	
	public function initTranslate() {
            DM_Locale::set( 'ru_RU' );
            DM_Translate::setDirectory( ROOT_PATH.'/translate' );
	}
	
	public function initRouter() {
			
            require_once ROOT_PATH.'/configs/routers/IndexRouter.php';

            DM_Request::addRewriteRule( new IndexRouter() ); // set index router
            DM_Request::addRewriteRule( new DM_Request_Router() );
	}
}

?>